package com.acwing.partition20;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/1/9 20:10
 */
public class AC1996打乱字母 {

    private static final int N = 50010;
    private static int n = 0;
    //分别存储原字符串列表，内部升序字符串列表和内部降序字符串列表
    private static String[] origin = new String[N], asc = new String[N], desc = new String[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        for (int i = 0; i < n; i++) {
            String s = reader.readLine();
            origin[i] = s;
            char[] arr = s.toCharArray();
            Arrays.sort(arr, 0, s.length());
            asc[i] = new String(arr);
            reverse(arr);
            desc[i] = new String(arr);
        }
        resolve();
        writer.flush();
    }

    private static void resolve() throws IOException {
        //对单词与单词之间进行排序
        Arrays.sort(asc, 0, n);
        Arrays.sort(desc, 0, n);
        for (int i = 0; i < n; i++) {
            String s = origin[i];
            char[] temp = s.toCharArray();
            Arrays.sort(temp);
            s = new String(temp);
            int left = 0, right = n - 1;
            while (left < right) {
                int mid = (left + right) >>> 1;
                if (s.compareTo(desc[mid]) <= 0) right = mid;
                else left = mid + 1;
            }
            writer.write((left + 1) + " ");
            left = 0;
            right = n - 1;
            reverse(temp);
            s = new String(temp);
            while (left < right) {
                int mid = (left + right + 1) >>> 1;
                if (s.compareTo(asc[mid]) >= 0) left = mid;
                else right = mid - 1;
            }
            writer.write((right + 1) + "\n");
        }
    }

    private static void reverse(char[] arr) {
        for (int i = 0; i < arr.length / 2; i++) {
            char t = arr[i];
            arr[i] = arr[arr.length - i - 1];
            arr[arr.length - i - 1] = t;
        }
    }
}
